package com.ficus.road.maintain.third.zh.score;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.zh.UnitScore;
import com.ficus.road.maintain.core.model.zh.UnitScoreZH;
import com.ficus.road.maintain.dao.UnitScoreDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.ficus.road.maintain.util.MyDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/8/19 下午8:09
 */
@Component
public class ZHUnitScore extends BaseZH {

    @Autowired
    private UnitScoreDao unitScoreDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjAssessmentScore";
        errMsg = "调用中海接口获取标段得分异常!";
        successMsg = "调用中海接口获取标段得分成功!";
    }

    @Scheduled(cron = "0 0 14 * * ?", zone = "GMT+8:00")
//    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    public void refreshUnitScore() {
        init();
        Date date = MyDateUtil.getFirstDayOfLastMonth();
        String yearMonth = MyDateUtil.dateFormatStr(date, "yyyy-MM");
        arg1 = "{\"type\":\"month\",\"info\":\"" + yearMonth + "\"}";
        String zhUnitScore = getZHData();
        if (StringUtils.isEmpty(zhUnitScore)) {
            return;
        }
        List<UnitScoreZH> unitScoreZhList = JSONArray.parseArray(zhUnitScore, UnitScoreZH.class);
        List<UnitScore> unitScoreList = unitScoreZhList.stream().map(item -> item.convert2UnitScore(item, yearMonth)).collect(Collectors.toList());
        unitScoreList.forEach(unitScoreDao::saveOrUpdate);
    }
}
